import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:gif/gif.dart';

import '../bean/BlockBean.dart';
import '../utils/CommUtils.dart';

//背景方格
class BackgroundBrik extends StatelessWidget {
  final BlockBean bean;
  final bool shine; //true表示闪耀

  final int xpositon; //棋盘横索引
  final int ypositon; //棋盘竖索引
  final List<HashMap<int, HashMap<int, BlockBean>>> bgChangelist;

  final List<HashMap<int, HashMap<int, BlockBean>>> scoreChangelist;

  const BackgroundBrik._(
      {Key? key,
      required this.bean,
      required this.bgChangelist,
      required this.scoreChangelist,
      required this.shine,
      required this.xpositon,
      required this.ypositon})
      : super(key: key);

  const BackgroundBrik.normal(
    BlockBean bean,
    List<HashMap<int, HashMap<int, BlockBean>>> bglist,
    List<HashMap<int, HashMap<int, BlockBean>>> scorelist,
    bool shine,
    int xpositon,
    int ypositon,
  ) : this._(
            bean: bean,
            bgChangelist: bglist,
            scoreChangelist: scorelist,
            shine: shine,
            xpositon: xpositon,
            ypositon: ypositon);

  @override
  Widget build(BuildContext context) {
    final size = CommUtils.getSize(context);
    return SizedBox.fromSize(
      size: size,
      child: getType(size),
    );
  }

  Widget getType(Size size) {
    if (shine && bgChangelist.isNotEmpty && bgChangelist.length > 0) {
      //需要闪耀的话
     
      bool addMeng = false;
      for (int i = 0; i < bgChangelist.length; i++) {
        HashMap map = bgChangelist[i];
        //里面的map加的时候永远只有一个，所以只需第一个
        if (xpositon == map.keys.first) {
          HashMap<int, BlockBean> map2 = map.values.first; //里面有竖poston,
          if (ypositon == map2.keys.first) {
            addMeng = true;
            break;
          }
        }
      }

      if (addMeng) {
         
        return Container(
          margin: EdgeInsets.all(0.02 * size.width),
          child: Stack(
            children: [
              Image.asset("assets/images/special_square_light.png",
                  width: size.width, height: size.height, fit: BoxFit.cover),
            ],
          ),
        );
      } else {
        return getNormal(size);
      }
      //闪耀家一个蒙版
    } else {
      //不需要闪耀正常逻辑
      return getNormal(size);
    }
  }

/**
 * 正常布局  也有d
 */
  Widget getNormal(Size size) {
    if (bean.normal != 1) {
      return Container(
        width: size.width,
        height: size.height,
        margin: EdgeInsets.all(0.02 * size.width),
        child: Stack(
          children: [
            Image.asset(bean.defaultImg, fit: BoxFit.fill,width: size.width,height: size.height,),
            getOther(size),
          ],
        ),
      );
    } else {
      //查询蒙版情况
      if (scoreChangelist.isNotEmpty && scoreChangelist.length > 0) {
        //需要闪耀的话
        bool addMeng = false;
        for (int i = 0; i < scoreChangelist.length; i++) {
          HashMap map = scoreChangelist[i];
          //里面的map加的时候永远只有一个，所以只需第一个
          if (xpositon == map.keys.first) {
            HashMap<int, BlockBean> map2 = map.values.first; //里面有竖poston,
            if (ypositon == map2.keys.first) {
              addMeng = true;
              break;
            }
          }
        }

        if (addMeng) {
          return Container(
            margin: EdgeInsets.all(0.02 * size.width),
            //  child:  getmengType(size)
            child: Stack(
              children: [
                Image.asset(bean.img,
                    width: size.width, height: size.height, fit: BoxFit.cover),
                Container(
                  width: size.width,
                  height: size.height,
                  color: Colors.white.withOpacity(0.4),
                ),
              ],
            ),
          );
        } else {
          return getFinal(size);
        }
        //闪耀家一个蒙版
      } else {
        //不需要闪耀正常逻辑
        return getFinal(size);
      }
    }
  }

/**
 * 添加闪烁或者消分特效
 */
  Widget getOther(Size size) {
    if (bean.normal == 2) {
      if (bean.eliminationEffect == 1) {
        // print("消分了哈");
        //消分特效
        return Text('');
        // return Center(
        //   child: Gif(
        //     image: AssetImage("assets/gif/1_1.gif"),
        //     autostart: Autostart.once,
        //   ),
        // );
      } else if (bean.eliminationEffect == 2) {
     
        //新手背景闪烁
         return Gif(
           width: size.width, height: size.height,
          image: AssetImage("assets/gif/shine.gif"),
          autostart: Autostart.loop,
         );
      } else {
        return Text('');
      }
    } else {
      return Text('');
    }
  }

  /**
   * 有宝石蒙版，和普通方块的蒙版效果  暂时不用
   */
  Widget getmengType(
    Size size,
  ) {
    if (bean.gem != 0) {
      return Stack(
        children: [
          Image.asset(bean.img,
              width: size.width, height: size.height, fit: BoxFit.cover),
          Container(
            width: size.width,
            height: size.height,
            color: Colors.white.withOpacity(0.4),
          ),
        ],
      );
    } else {
      return Image.asset("assets/images/square_light.png",
          width: size.width, height: size.height, fit: BoxFit.cover);
    }
  }

  /**
   * 棋盘有方格和宝石
   */
  Widget getFinal(Size size) {
    return Container(
      margin: EdgeInsets.all(0.02 * size.width),
      // padding: EdgeInsets.all(0.1 * size.width),
      // decoration: BoxDecoration(
      //     //邊框
      //     border: Border.all(width: 0.10 * size.width, color: color)),
      child: Container(
        child: Image.asset(bean.img,
            width: size.width, height: size.height, fit: BoxFit.cover),
      ),
    );
  }
}

// //方框的縮小倍數
const double FixCount5 = 0.5;

const double FixCount1 = 1;

//随机圖片
class randomBrik extends StatelessWidget {
  final BlockBean bean;
  final double fixCount; //放大配送

  const randomBrik._({
    Key? key,
    required this.bean,
    required this.fixCount,
  }) : super(key: key);

  const randomBrik.normal(
    BlockBean bean,
    double FixCount,
  ) : this._(bean: bean, fixCount: FixCount);

  @override
  Widget build(BuildContext context) {
    final size = CommUtils.getSize(context) * fixCount; //縮小尺寸，以免屏幕不夠用
    return SizedBox.fromSize(size: size, child: getld(size));
  }

  Widget getld(Size size) {
    if (bean.normal == 1) {
      //顯示圖片
      return Container(
        margin: EdgeInsets.all(0.02 * size.width),
        // decoration: BoxDecoration(boxShadow: [
        //   BoxShadow(
        //       color: Color.fromRGBO(224, 222, 222, 1), //陰影顏色
        //       offset: Offset(8.0, 5.0), //阴影xy轴偏移量
        //       blurRadius: 1.0, //阴影模糊程度 越大越模糊
        //       spreadRadius: 0.5 //阴影扩散程度
        //       )
        // ]),
        child: Image.asset(bean.img,
            width: size.width, height: size.height, fit: BoxFit.cover),
      );
    } else if (bean.normal == 2) {
      //不满足填充情况
      return Container(
          margin: EdgeInsets.all(0.02 * size.width),
          child: Image.asset("assets/images/square_an.png",
              width: size.width, height: size.height, fit: BoxFit.cover));
    } else {
      //不是1的透明
      return Container(
        margin: EdgeInsets.all(0.02 * size.width),
        // decoration: BoxDecoration( //邊框 顏色，這裡不加，再棋盤中增加
        //   border: Border(
        //     right: BorderSide(
        //         width: 0.05 * size.width, color: Color(0xFFFF7F7F7F)),
        //     bottom: BorderSide(
        //         width: 0.05 * size.width, color: Color(0xFFFF7F7F7F)),
        //   ),
        // ),

        child: Container(
          //透明即可
          color: Colors.transparent,
        ),
      );
    }
  }
}
